package com.application.bisabahasakorea;

import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemLongClickListener;

public class TerminalKedatanganActivity extends ListActivity {
	private MyDatabaseHelper dbhelper;
	private SQLiteDatabase db = null;
	private MediaPlayer player;
	
    /** Called when the activity is first created. */
    @Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		dbhelper = new MyDatabaseHelper(this);
		db = dbhelper.getWritableDatabase();
		dbhelper.delAllData(db);
		dbhelper.generateDataTerminalKedatangan(db);
		setContentView(R.layout.percakapan);
		
		// add title of activity
		TextView txtTitle = (TextView)findViewById(R.id.txtjudul);
		txtTitle.setText("TERMINAL KEDATANGAN");
		
		isDataListView();
		
		//Long click handler
		OnItemLongClickListener listener = new OnItemLongClickListener() {
          //  @Override
            public boolean onItemLongClick(AdapterView<?> arg0, View v, int position, long id) {
            	TextView key = (TextView)v.findViewById(R.id.txtID);
        		String s = key.getText().toString();
        		dbhelper.updateDataTerminal(db, s);
                Toast.makeText(getBaseContext(), "Telah ditambahkan ke Favorite!" , Toast.LENGTH_SHORT).show();
                return false;
            }
        };
        getListView().setOnItemLongClickListener(listener);
	}
    
    @Override
	protected void onListItemClick(ListView l, View v, int position, long id){
    	v.setSelected(true);
		super.onListItemClick(l, v, position, id);
        // Get the item that was clicked
		TextView urlvoice = (TextView)v.findViewById(R.id.txtvoice_percakapan);
		String s = urlvoice.getText().toString();
		playSound(s);
	}
    
    private void isDataListView() {
    	Cursor dataCursor;
    	
    	dataCursor = dbhelper.fetchDataTerminalKedatangan(db);

    	startManagingCursor( dataCursor);
    	/*Create an array to specify the fields we want to display in the list (only the 'NAMA' column in this case) */
    	String[] from = new String[]{MyDatabaseHelper.BAHASA, MyDatabaseHelper.HANGUL, MyDatabaseHelper.ROMAJI, MyDatabaseHelper.V_HANGUL, MyDatabaseHelper.KEY_ID}; 
    	/* and an array of the fields we want to bind those fields */
    	int[] to = new int[]{R.id.txtbahasa, R.id.txthangul, R.id.txtromaji, R.id.txtvoice_percakapan, R.id.txtID};

    	/* Now create a simple cursor adapter.. */ 
    	SimpleCursorAdapter dataAdapter =
    	new SimpleCursorAdapter(this, R.layout.row_percakapan, dataCursor, from, to);

    	/* and assign it to our Spinner widget */ 
    	this.setListAdapter(dataAdapter);
    	}
    
    @Override
	public void onDestroy() {
		super.onDestroy();
		try {		
			db.close();
		}catch (Exception e){
			
		}
	}
    
    private void playSound(String resource){
    	Context context = getBaseContext();
    	int identifier = getResources().getIdentifier(resource, "raw", context.getPackageName());
    	try{
    		if(player.isPlaying()){
    			player.stop();
    			player.release();
    		}
    	}catch(Exception e){
    		//	Do Nothing
    	}
    	player = MediaPlayer.create(this, identifier);
    	player.setLooping(false); // Set looping
        player.start();

    }
    @Override
    protected void onPause() {
        super.onPause(); 
        try {
        	player.pause();
        } catch (Exception e){
        //Do nothing
        }
    }
}
